/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.tenant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.market.base.tenant.controller.assembler.TenantMenuAssembler;
import net.siufung.market.base.tenant.controller.model.dto.TenantMenuSaveDto;
import net.siufung.market.base.tenant.controller.model.vo.TenantMenuVo;
import net.siufung.market.base.tenant.controller.regulation.TenantMenuCreateBatchRegulation;
import net.siufung.market.base.tenant.mapper.ITenantMenuMapper;
import net.siufung.market.base.tenant.mapper.entity.TenantMenu;
import net.siufung.market.base.tenant.service.ITenantMenuService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统菜单租户表 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-06 14:45:08
 */
@Api(tags = "系统菜单租户表")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/tenant/menu")
public class TenantMenuController {

    private final ITenantMenuMapper tenantMenuMapper;
    private final ITenantMenuService tenantMenuService;

    /**
     * 获取 系统菜单租户表列表
     * @param tenantId 属性
     * @return Result<List<TenantMenuVo>>
     * */
    @ApiOperation(value = "获取系统菜单租户表列表")
    @PostMapping(value = "/list/{tenantId}")
    public Result<List<TenantMenuVo>> list(
            @PathVariable("tenantId") Integer tenantId) {
        return Result.ok(
            TenantMenuAssembler.fromList(
                    tenantMenuMapper.selectList(
                            new LambdaQueryWrapper<TenantMenu>()
                                    .eq(TenantMenu::getTenantId, tenantId))));
    }

    /**
     * 创建 系统菜单租户表
     * @param tenantMenuSaveDto 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "创建 系统菜单租户表")
    @PostMapping(value="/createBatch")
    public Result<Void> createBatch(
            @RequestBody TenantMenuSaveDto tenantMenuSaveDto) {
        tenantMenuSaveDto.validated();
        List<TenantMenu> tenantMenuList = tenantMenuSaveDto.toList();
        TenantMenuCreateBatchRegulation tenantMenuCreateBatchRegulation
                = new TenantMenuCreateBatchRegulation();
        tenantMenuCreateBatchRegulation.isSatisfiedBy(tenantMenuList);
;       if(tenantMenuService.saveBatch(tenantMenuList)){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }



}